package com.yqman.daoImpl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map.Entry;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import com.yqman.dao.IConnection;
import com.yqman.dbc.ManagementConnection;
import com.yqman.domain.CardInformation;
import com.yqman.entity.DatabaseVisitResult;

public class CardInformationConnection implements IConnection<CardInformation>{
	private ManagementConnection driver;
	private static Logger logger = Logger.getLogger(CardInformationConnection.class);

	public CardInformationConnection(){
		driver = ManagementConnection.getInstance();
		BasicConfigurator.configure(); 
	}
	@Override
	public DatabaseVisitResult insertData(CardInformation t) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DatabaseVisitResult deleteData(CardInformation t) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DatabaseVisitResult updateData(CardInformation t) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CardInformation queryData(HashMap<String, String> map) {
		if(map==null||map.isEmpty()) return null;
		java.sql.ResultSet rs = null;
		CardInformation info = null;
		StringBuilder  builder  = new StringBuilder();
		builder.append("SELECT * FROM card_information WHERE ");
		for(Entry<String, String> entry: map.entrySet()){
			builder.append(entry.getKey());
			builder.append("='");
			builder.append(entry.getValue());
			builder.append("'");
			builder.append(" and ");
		}
		builder.delete(builder.length()-4,builder.length());
		rs = driver.executeQuery(builder.toString());
		try {
			if(rs!=null&&rs.next()){
				info = new CardInformation();
				info.setId(rs.getInt(1));
				info.setType(rs.getInt(2));
				info.setYears(rs.getInt(3));
				info.setTimes(rs.getInt(4));
				info.setStart_time(rs.getDate(5));
				info.setEnd_time(rs.getDate(6));
			}
		} catch (SQLException e) {
			logger.warn("queryData()"+e.getMessage());
			e.printStackTrace();
		}
		return info;
	}
	
}
